package com.myprocessing.box.button.drag;

import com.myprocessing.box.Box;
import com.myprocessing.box.button.Button;
import com.myprocessing.gui.util.Coord;

public class DragButton extends Button {

	protected boolean dragable = true;
	protected boolean drag;
	protected Coord dragPosition;
	protected DragPositionManager dragPositionManager;
	
	public void init() {
		super.init();
		dragPosition = position;
	}

	public void display() {
		if(drag){
			setPosition(new Coord(pApplet.mouseX - size.getX()/2, pApplet.mouseY - size.getY()/2));
		}
		super.display();
	}
	public boolean isDrag() {
		return drag;
	}

	public void setDrag(boolean drag) {
		this.drag = drag;
	}
	
	public void toggleDrag(){
		drag = !drag;
		System.out.println("TOOGLE");
		
		if(!drag){
			Coord newPos = dragPositionManager.getValidCoord(position);
			
			if(newPos != null){
				dragPosition = newPos;
				position = newPos;

				dragPositionManager.execEndDrag(dragPosition);
			}

			setPosition(dragPosition);
		} else{
			dragPositionManager.execStartDrag(dragPosition);
		}
	}
	
	public void onMousePress() {
//		System.out.println(pApplet.mouseX + " " + pApplet.mouseY);
//		System.out.println(getAbsolutePosition().getX() + " " + getAbsolutePosition().getY());
//		System.out.println(size.getX() + " " + size.getY());
//		System.out.println("a------------------------------------");
		if(isValidMouseOver() && dragable){
			executeAction(mousePress);

			for (Box box : subBoxSet) {
				box.onMousePress();
			}
			toggleDrag();
		}
	}

	public DragPositionManager getDragPositionManager() {
		return dragPositionManager;
	}

	public void setDragPositionManager(DragPositionManager dragPositionManager) {
		this.dragPositionManager = dragPositionManager;
	}

	public boolean isDragable() {
		return dragable;
	}

	public void setDragable(boolean dragable) {
		this.dragable = dragable;
	}
}
